package com.ai.bitcoin.jpa.dao;

import com.ai.bitcoin.constant.Bcc;
import com.ai.bitcoin.jpa.entity.BccBasicInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * @author Jason
 * @date 2018/2/4
 */
public interface BccBasicInfoDao extends JpaRepository<BccBasicInfo, String> {

    //根据bccId取得bcc信息
    BccBasicInfo findByBccId(String bccId);

    BccBasicInfo findBySymbol(String symbol);

    List<BccBasicInfo> findByBccIdAndRecordState(String bccId, String recordState);


    List<BccBasicInfo> findByRecordState(String bcState);

    @Query(nativeQuery = true, value = "update bcc_basic_info set record_state ='" + Bcc.RecordState.PUBLISH + "' where batch_no = ?1")
    @Modifying
    void update2Publish(String unPublishBatch);

    @Query(nativeQuery = true, value = "delete  from bcc_basic_info where record_state ='" + Bcc.RecordState.UN_PUBLISH + "'")
    @Modifying
    void deleteNoPublish();

    @Query(nativeQuery = true, value = "delete  from bcc_basic_info where batch_no<> ?1 or record_state ='" + Bcc.RecordState.PUBLISH + "'")
    @Modifying
    void deleteOldPublish(String publishBatch);

    @Query(nativeQuery = true, value = "SELECT *  from bcc_basic_info where record_state = ?1 limit 1")
    BccBasicInfo findOneByRecordState(String recordState);

    @Query(nativeQuery = true, value = "SELECT *  from bcc_basic_info where record_state = '" + Bcc.RecordState.PUBLISH + "' and bcc_id in (?1) ")
    List<BccBasicInfo> queryBccBasicInfos(List<String> bccIds);

    /**
     * 查询所有bccId列表
     *
     * @return 所有bccId列表
     */
    @Query("SELECT e.bccId FROM BccBasicInfo e")
    List<String> findAllBccId();


    /**
     * 查询所有bccId列表
     *
     * @return 所有bccId列表
     */
    @Query("SELECT e.symbol FROM BccBasicInfo e")
    List<String> findAllBccSymbol();

    /**
     * 用户未订阅币列表
     *
     * @param openId 用户openId
     * @return 币列表
     */
    @Query(nativeQuery = true, value = "SELECT bcc.* FROM bcc_basic_info bcc WHERE NOT EXISTS(SELECT 1 FROM bcc_sub_coin sub WHERE sub.bcc_id = bcc.bcc_id AND user_open_id = :openId AND status = 1 AND end_time >= UNIX_TIMESTAMP())")
    List<BccBasicInfo> findUserUnsubscribedList(@Param(value = "openId") String openId);

    /**
     * 用户已订阅币列表
     *
     * @param openId 用户openId
     * @return 币列表
     */
    @Query(nativeQuery = true, value = "SELECT bcc.* FROM bcc_basic_info bcc WHERE EXISTS(SELECT 1 FROM bcc_sub_coin sub WHERE sub.bcc_id = bcc.bcc_id AND user_open_id = ?1 AND status = 1 AND end_time >= UNIX_TIMESTAMP())")
    List<BccBasicInfo> findUserSubscribedList(String openId);

    @Query("SELECT e FROM BccBasicInfo e WHERE e.nameCn = :keyword OR e.nameEn = :keyword OR e.symbol = :keyword")
    List<BccBasicInfo> findByKeyword(@Param("keyword") String keyword);
}
